package com.crealth.db;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.crealth.ConstantDefine;
import com.crealth.Result.FollowupConsumerResult;
import com.crealth.manager.MybatisManager;
import com.crealth.model.ConsumerCarModel;
import com.crealth.model.ConsumerCreditCardModel;
import com.crealth.model.ConsumerFollowupModel;
import com.crealth.model.ConsumerHouseModel;
import com.crealth.model.ConsumerInfoModel;
import com.crealth.model.ConsumerLoanModel;
import com.crealth.model.ConsumerNextFollowupModel;
import com.crealth.model.ConsumerPolicyModel;
import com.crealth.model.ConsumerSelfModel;
import com.crealth.model.ConsumerWorkModel;
import com.crealth.utils.Utils;

public class ConsumerDb {
	private static Logger logger = Logger.getLogger("running_log");
	private static Logger errorLog = Logger.getLogger("error_log");
	
	private static void fillConsumerData(ConsumerInfoModel model) {
		if (model == null) {
			return;
		}
		try {
			int consumerID = model.getId();
			List<ConsumerHouseModel> houses = selectConsumerHouse(consumerID);
			List<ConsumerCarModel> cars = selectConsumerCar(consumerID);
			List<ConsumerPolicyModel> policys = selectConsumerPolicy(consumerID);
			List<ConsumerLoanModel> loans = selectConsumerLoan(consumerID);
			List<ConsumerCreditCardModel> creditCards = selectConsumerCreditCard(consumerID);
			ConsumerSelfModel selfCompany = selectConsumerSelf(consumerID);
			ConsumerWorkModel work = selectConsumerWork(consumerID);
			List<ConsumerFollowupModel> followupls = selectConsumerFollowup(consumerID);
			List<ConsumerNextFollowupModel> nextFollowupls = selectNextFollowup(consumerID);
			model.setHousels(houses);
			model.setCarls(cars);
			model.setPolicyls(policys);
			model.setLoanls(loans);
			model.setCreditCardls(creditCards);
			model.setSelfModel(selfCompany);
			model.setWorkModel(work);
			model.setFollowupls(followupls);
			model.setNextFollowupls(nextFollowupls);
		} catch (Exception e) {
    		errorLog.error("fillConsumerData", e);
    	}
	}
	
	public static List<ConsumerInfoModel> selectConsumerInfoByUserID(int userID, int downMinid) {
		List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper.
				selectConsumerInfoByUserID(userID, downMinid);
		for (ConsumerInfoModel model : consumerls) {
			fillConsumerData(model);
		}
		
		return consumerls;
	}
	public static List<ConsumerInfoModel> selectConsumerInfoByUserIDs(String userIDs, int downMinid) {
		List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
				.selectConsumerInfoByUserIDs(userIDs, downMinid);
		for (ConsumerInfoModel model : consumerls) {
			fillConsumerData(model);
		}
		
		return consumerls;
	}
	
	public static List<ConsumerInfoModel> selectAllConsumerInfo(int downMinid, boolean needDetail) {
		try {
			List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
					.selectAllConsumerInfo(downMinid);
			if (needDetail) {
				for (ConsumerInfoModel model : consumerls) {
					fillConsumerData(model);
				}
			}
			return consumerls;
		} catch (Exception e) {
    		errorLog.error("", e);
    		return null;
    	}
	}
	
	public static List<ConsumerInfoModel> selectAllConsumer(boolean needDetail) {
		int downMinid = Integer.MAX_VALUE;
		try {
			List<ConsumerInfoModel> allReslutls = new ArrayList<ConsumerInfoModel>();
			while (true) {
				List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
						.selectAllConsumerInfo(downMinid);
				if (consumerls == null || consumerls.size() == 0) {
					break;
				}
				downMinid = consumerls.get(consumerls.size()-1).getId();
				if (Utils.isWindows()) {
					logger.info("selectAllConsumer  downMinid: " + downMinid);
				}
				if (needDetail) {
					for (ConsumerInfoModel model : consumerls) {
						fillConsumerData(model);
						allReslutls.add(model);
					}
				}
			}
			return allReslutls;
		} catch (Exception e) {
    		errorLog.error("", e);
    		return null;
    	}
	}
	
	public static List<ConsumerInfoModel> selectConsumerInfoByUserIDTm(int userID, Timestamp localTm) {
		List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
				.selectConsumerInfoByUserIDTm(userID, localTm);
		for (ConsumerInfoModel model : consumerls) {
			fillConsumerData(model);
		}
		
		return consumerls;
	}
	
	public static List<ConsumerInfoModel> selectConsumerInfoByUserIDsTm(String userIDs, Timestamp localTm) {
		List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
				.selectConsumerInfoByUserIDsTm(userIDs, localTm);
		for (ConsumerInfoModel model : consumerls) {
			fillConsumerData(model);
		}
		
		return consumerls;
	}
	public static List<ConsumerInfoModel> selectAllConsumerInfoTm(Timestamp localTm) {
		try {
			List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
					.selectAllConsumerInfoTm(localTm);
			for (ConsumerInfoModel model : consumerls) {
				fillConsumerData(model);
			}
			return consumerls;
		} catch (Exception e) {
    		errorLog.error("", e);
    		return null;
    	}
	}
	
	public static List<ConsumerInfoModel> selectTodaySubmitConsumer(String strToday) {
		List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper.selectTodaySubmitConsumer(strToday);
		
		return consumerls;
	}
	
	public static Integer updateSendBackConsumer(int id) {
		Integer res = MybatisManager.consumerMapper.updateSendBackConsumer(id);
		MybatisManager.commit();
		
		return res;
	}
	
	public static List<Integer> selectConsumerIDByUserID(int userID) {
		List<Integer> cmrIDls = MybatisManager.consumerMapper.selectConsumerIDByUserID(userID);
		
		return cmrIDls;
	}
	public static List<Integer> selectConsumerIDByIDs(String userIDs) {
		List<Integer> cmrIDls = MybatisManager.consumerMapper.selectConsumerIDByIDs(userIDs);
		
		return cmrIDls;
	}
	public static List<Integer> selectAllConsumerID() {
		List<Integer> cmrIDls = MybatisManager.consumerMapper.selectAllConsumerID();
		
		return cmrIDls;
	}
	
	public static List<Integer> selectUserConsumerIDs(int userID) {
		List<Integer> idls = MybatisManager.consumerMapper.selectUserConsumerIDs(userID);
		
		return idls;
	}
	
	public static List<Integer> selectConsumerIDByUserIDLimit(int userID, int downMinid) {
		List<Integer> cmrIDls = MybatisManager.consumerMapper.selectConsumerIDByUserIDLimit(userID, downMinid);
		
		return cmrIDls;
	}
	public static List<Integer> selectConsumerIDByUserIDsLimit(String userIDs, int downMinid) {
		List<Integer> cmrIDls = MybatisManager.consumerMapper.selectConsumerIDByUserIDsLimit(userIDs, downMinid);
		
		return cmrIDls;
	}
	public static List<Integer> selectAllConsumerIDLimit(int downMinid) {
		List<Integer> cmrIDls = MybatisManager.consumerMapper.selectAllConsumerIDLimit(downMinid);
		
		return cmrIDls;
	}
	
    public static Integer insertConsumerInfoObj(ConsumerInfoModel consumerInfoModel) {
    	try {
	    	MybatisManager.consumerMapper.insertConsumerInfoObj(consumerInfoModel);
	    	MybatisManager.commit();
	    	int consumerID = consumerInfoModel.getId();
	    	if (consumerID <= 0) {
	    		errorLog.error("insertConsumerInfoObj  fail consumerID: "+consumerID);
	    		return null;
	    	}
	    	consumerInfoModel.setId(consumerID);
	    	List<ConsumerHouseModel> housels = consumerInfoModel.getHousels();
	    	List<ConsumerCarModel> carls = consumerInfoModel.getCarls();
	    	List<ConsumerPolicyModel> policyls = consumerInfoModel.getPolicyls();
	    	ConsumerWorkModel workModel = consumerInfoModel.getWorkModel();
	    	ConsumerSelfModel selfModel = consumerInfoModel.getSelfModel();
	    	List<ConsumerLoanModel> loanls = consumerInfoModel.getLoanls();    	
	    	List<ConsumerCreditCardModel> creditCardls = consumerInfoModel.getCreditCardls();
	    	
	    	if (housels != null && housels.size() > 0) {
	    		for (ConsumerHouseModel house : housels) {
	    			house.setConsumerID(consumerID);
	    			insertConsumerHouse(house);
	    		}
	    	}
	    	if (carls != null && carls.size() > 0) {
	    		for (ConsumerCarModel car : carls) {
	    			car.setConsumerID(consumerID);
	    			insertConsumerCar(car);
	    		}
	    	}
	    	if (policyls != null && policyls.size() > 0) {
	    		for (ConsumerPolicyModel policy : policyls) {
	    			policy.setConsumerID(consumerID);
	    			insertConsumerPolicy(policy);
	    		}
	    	}
	    	if (workModel != null) {
	    		workModel.setConsumerID(consumerID);
	    		insertConsumerWork(workModel);
	    	}
	    	if (selfModel != null) {
	    		selfModel.setConsumerID(consumerID);
	    		insertConsumerSelf(selfModel);
	    	}
	    	if (loanls != null && loanls.size() > 0) {
	    		for (ConsumerLoanModel loan : loanls) {
	    			loan.setConsumerID(consumerID);
	    			insertConsumerLoan(loan);
	    		}
	    	}
	    	if (creditCardls != null && creditCardls.size() > 0) {
	    		for (ConsumerCreditCardModel card : creditCardls) {
	    			card.setConsumerID(consumerID);
	    			insertConsumerCreditCard(card);
	    		}
	    	}
	    	
	    	String followupInfo = consumerInfoModel.getFollowupInfo();
	    	if (followupInfo != null && followupInfo.length() > 0) {
	    		ConsumerFollowupModel followup = new ConsumerFollowupModel();
	    		followup.setConsumerID(consumerID);
	    		followup.setFollowupIfno(followupInfo);
	    		insertConsumerFollowup(followup);
	    	}
	    	
	    	return consumerID;
    	}
    	
    	catch (Exception e) {    		
    		errorLog.error("userID:"+consumerInfoModel.getUserID()+" "+consumerInfoModel.getName()+consumerInfoModel.getPhoneNum1());
    		errorLog.error("", e);
    	}
    	return null;
    }
    
    public static Integer insertImportPurposeExcel(ConsumerInfoModel consumerInfoModel) {
    	try {
	    	MybatisManager.consumerMapper.insertImportPurposeExcel(consumerInfoModel);
	    	MybatisManager.commit();
	    	Integer resId = consumerInfoModel.getId();
	    	if (resId != null && resId > 0) {
		    	if (consumerInfoModel.getFollowupInfo().length() > 0) {
		    		if (consumerInfoModel.getLastfollowupTm() == null) {
		    			consumerInfoModel.setLastfollowupTm(new Timestamp(System.currentTimeMillis()));
		    		}
		    		
		    		ConsumerFollowupModel followup = new ConsumerFollowupModel();
		    		followup.setConsumerID(resId);
		    		followup.setFollowupIfno(consumerInfoModel.getFollowupInfo());
		    		followup.setFollowupTm(consumerInfoModel.getLastfollowupTm());
		    		insertConsumerFollowupTm(followup);
		    	}
	    	}	    	
	    	return resId;
    	} catch (Exception e) {
    		errorLog.error("", e);    		
    	}
    	return null;
    }
    
    public static Integer updateConsumerInfoObj(ConsumerInfoModel consumerInfoModel, ConsumerInfoModel existModel, int position) {
    	try {
    		if (position == ConstantDefine.Installmenter) {
    			MybatisManager.consumerMapper.updateConsumerInfoByMortgage(consumerInfoModel);
    		} else {
    			MybatisManager.consumerMapper.updateConsumerInfoByID(consumerInfoModel);
    		}
	    	MybatisManager.commit();
	    	int consumerID = consumerInfoModel.getId();
	    	List<ConsumerHouseModel> housels = consumerInfoModel.getHousels();
	    	List<ConsumerCarModel> carls = consumerInfoModel.getCarls();
	    	List<ConsumerPolicyModel> policyls = consumerInfoModel.getPolicyls();
	    	ConsumerWorkModel workModel = consumerInfoModel.getWorkModel();
	    	ConsumerSelfModel selfModel = consumerInfoModel.getSelfModel();
	    	List<ConsumerLoanModel> loanls = consumerInfoModel.getLoanls();    	
	    	List<ConsumerCreditCardModel> creditCardls = consumerInfoModel.getCreditCardls();
	    	//  更新房子
	    	int existNum = 0;
	    	if (existModel.getHousels() != null) {
    			existNum = existModel.getHousels().size();
    		}
	    	int newNum = 0;
	    	int smaller = (existNum > newNum) ? newNum : existNum;	    	
	    	if (housels != null && housels.size() > 0) {
	    		newNum = housels.size();
	    		smaller = (existNum > newNum) ? newNum : existNum;
	    		for (int i = 0; i < smaller; ++i) {
	    			ConsumerHouseModel house = housels.get(i);
	    			house.setId(existModel.getHousels().get(i).getId());
	    			house.setConsumerID(consumerID);
	    			updateConsumerHouse(house);
	    		}
	    		if (newNum > smaller) {
	    			for (int i = smaller; i < newNum; ++i) {
		    			ConsumerHouseModel house = housels.get(i);		    			
		    			house.setConsumerID(consumerID);
		    			insertConsumerHouse(house);
		    		}
	    		}
	    	}
	    	if (existNum > smaller) {
    			for (int i = smaller; i < existNum; ++i) {
	    			ConsumerHouseModel house = existModel.getHousels().get(i);
	    			deleteConsumerHouse(house.getId(), house.getConsumerID());
	    		}
    		}
	    	
	    	//  更新 车
	    	existNum = 0;
	    	if (existModel.getCarls() != null) {
    			existNum = existModel.getCarls().size();
    		}
	    	newNum = 0;
	    	smaller = (existNum > newNum) ? newNum : existNum;
	    	if (carls != null && carls.size() > 0) {
	    		newNum = carls.size();
	    		smaller = (existNum > newNum) ? newNum : existNum;
	    		for (int i = 0; i < smaller; ++i) {
	    			ConsumerCarModel car = carls.get(i);
	    			car.setId(existModel.getCarls().get(i).getId());
	    			car.setConsumerID(consumerID);
	    			updateConsumerCar(car);
	    		}
	    		if (newNum > smaller) {
	    			for (int i = smaller; i < newNum; ++i) {
		    			ConsumerCarModel car = carls.get(i);		    			
		    			car.setConsumerID(consumerID);
		    			insertConsumerCar(car);
		    		}
	    		}
	    	}
    		if (existNum > smaller) {
    			for (int i = smaller; i < existNum; ++i) {
	    			ConsumerCarModel car = existModel.getCarls().get(i);
	    			deleteConsumerCar(car.getId(), car.getConsumerID());
	    		}
    		}

    		//  更新 保单
    		existNum = 0;
	    	if (existModel.getPolicyls() != null) {
    			existNum = existModel.getPolicyls().size();
    		}
	    	newNum = 0;
	    	smaller = (existNum > newNum) ? newNum : existNum;
	    	if (policyls != null && policyls.size() > 0) {
	    		newNum = policyls.size();
	    		smaller = (existNum > newNum) ? newNum : existNum;
	    		for (int i = 0; i < smaller; ++i) {
	    			ConsumerPolicyModel policy = policyls.get(i);
	    			policy.setId(existModel.getPolicyls().get(i).getId());
	    			policy.setConsumerID(consumerID);
	    			updateConsumerPolicy(policy);
	    		}
	    		if (newNum > smaller) {
	    			for (int i = smaller; i < newNum; ++i) {
		    			ConsumerPolicyModel policy = policyls.get(i);		    			
		    			policy.setConsumerID(consumerID);
		    			insertConsumerPolicy(policy);
		    		}
	    		}
	    	}
	    	if (existNum > smaller) {
    			for (int i = smaller; i < existNum; ++i) {
	    			ConsumerPolicyModel policy = existModel.getPolicyls().get(i);
	    			deleteConsumerPolicy(policy.getId(), policy.getConsumerID());
	    		}
    		}
	    	
	    	//  更新 贷款
	    	existNum = 0;
	    	if (existModel.getLoanls() != null) {
    			existNum = existModel.getLoanls().size();
    		}
	    	newNum = 0;
	    	smaller = (existNum > newNum) ? newNum : existNum;
	    	if (loanls != null && loanls.size() > 0) {
	    		newNum = loanls.size();
	    		smaller = (existNum > newNum) ? newNum : existNum;
	    		for (int i = 0; i < smaller; ++i) {
	    			ConsumerLoanModel loan = loanls.get(i);
	    			loan.setId(existModel.getLoanls().get(i).getId());
	    			loan.setConsumerID(consumerID);
	    			updateConsumerLoan(loan);
	    		}
	    		if (newNum > smaller) {
	    			for (int i = smaller; i < newNum; ++i) {
		    			ConsumerLoanModel loan = loanls.get(i);		    			
		    			loan.setConsumerID(consumerID);
		    			insertConsumerLoan(loan);
		    		}
	    		}
	    	}
	    	if (existNum > smaller) {
    			for (int i = smaller; i < existNum; ++i) {
	    			ConsumerLoanModel loan = existModel.getLoanls().get(i);
	    			deleteConsumerLoan(loan.getId(), loan.getConsumerID());
	    		}
    		}
	    	
	    	//  更新 信用卡
	    	existNum = 0;
	    	if (existModel.getCreditCardls() != null) {
    			existNum = existModel.getCreditCardls().size();
    		}
	    	newNum = 0;
	    	smaller = (existNum > newNum) ? newNum : existNum;
	    	if (creditCardls != null && creditCardls.size() > 0) {
	    		newNum = creditCardls.size();
	    		smaller = (existNum > newNum) ? newNum : existNum;
	    		for (int i = 0; i < smaller; ++i) {
	    			ConsumerCreditCardModel creditCard = creditCardls.get(i);
	    			creditCard.setId(existModel.getCreditCardls().get(i).getId());
	    			creditCard.setConsumerID(consumerID);
	    			updateConsumerCreditCard(creditCard);
	    		}
	    		if (newNum > smaller) {
	    			for (int i = smaller; i < newNum; ++i) {
	    				ConsumerCreditCardModel creditCard = creditCardls.get(i);		    			
	    				creditCard.setConsumerID(consumerID);
		    			insertConsumerCreditCard(creditCard);
		    		}
	    		}
	    	}
	    	if (existNum > smaller) {
    			for (int i = smaller; i < existNum; ++i) {
    				ConsumerCreditCardModel creditCard = existModel.getCreditCardls().get(i);
	    			deleteConsumerCreditCard(creditCard.getId(), creditCard.getConsumerID());
	    		}
    		}
	    		    	
	    	if (workModel != null && existModel.getWorkModel() != null) {
	    		workModel.setConsumerID(consumerID);
	    		workModel.setId(existModel.getWorkModel().getId());
	    		updateConsumerWork(workModel);
	    	}
	    	if (selfModel != null && existModel.getSelfModel() != null) {
	    		selfModel.setConsumerID(consumerID);
	    		selfModel.setId(existModel.getSelfModel().getId());
	    		updateConsumerSelf(selfModel);
	    	}
	    	if (workModel != null && existModel.getWorkModel() == null) {
	    		workModel.setConsumerID(consumerID);
	    		insertConsumerWork(workModel);
	    		ConsumerSelfModel oldSelf = existModel.getSelfModel();
	    		if (oldSelf != null) {
	    			deleteConsumerSelf(oldSelf.getId(), oldSelf.getConsumerID());
	    		}
	    	}
	    	if (selfModel != null && existModel.getSelfModel() == null) {
	    		selfModel.setConsumerID(consumerID);
	    		insertConsumerSelf(selfModel);
	    		ConsumerWorkModel oldWork = existModel.getWorkModel();
	    		if (oldWork != null) {
	    			deleteConsumerWork(oldWork.getId(), oldWork.getConsumerID());
	    		}
	    	}
	    	
	    	String followupInfo = consumerInfoModel.getFollowupInfo();
	    	if (followupInfo != null && followupInfo.length() > 0) {
	    		ConsumerFollowupModel followup = new ConsumerFollowupModel();
	    		followup.setConsumerID(consumerID);
	    		followup.setFollowupIfno(followupInfo);
	    		insertConsumerFollowup(followup);
	    	}
	    	
	    	return consumerID;
    	}    	
    	catch (Exception e) {
    		errorLog.error("", e);
    	}
    	return null;
    }
    
    public static Integer selectConsumerInfoByNameAndPhone(int userID, String cliName, String phoneNum1) {
    	Integer modelID = MybatisManager.consumerMapper.selectConsumerInfoByNameAndPhone(userID, cliName, phoneNum1);
    	    	
    	return modelID;
    }
    
    public static Integer selectConsumerInfoByPhone(int userID, String phoneNum1) {
    	Integer modelID = MybatisManager.consumerMapper.selectConsumerInfoByPhone(userID, phoneNum1);
    	    	
    	return modelID;
    }
    
    public static ConsumerInfoModel selectConsumerInfoByID(int consumerID) {
    	ConsumerInfoModel model = MybatisManager.consumerMapper.selectConsumerInfoByID(consumerID);
    	if (model == null) {
    		return null;
    	}
    	fillConsumerData(model);
		
    	return model;
    }
    
    public static Integer updateConsumerLeaderGuideByID(ConsumerInfoModel consumerInfoModel) {
    	Integer res = MybatisManager.consumerMapper.updateConsumerLeaderGuideByID(consumerInfoModel);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static ConsumerInfoModel selectConsumerLeaderGuideByID(int consumerID) {
    	ConsumerInfoModel model = MybatisManager.consumerMapper.selectConsumerLeaderGuideByID(consumerID);
    	
    	return model;
    }
    
    public static void updateConsumerNameAndPhone(int consumerID,
    		String strName, String phoneNum1) {
    	MybatisManager.consumerMapper.updateConsumerNameAndPhone(consumerID, strName, phoneNum1);
    	MybatisManager.commit();
    }
    
    public static void updateConsumerStatusByID(int consumerID, int status) {
    	MybatisManager.consumerMapper.updateConsumerStatusByID(consumerID, status);
    	MybatisManager.commit();
    }
    
    public static Integer updateBelongCompanyByID(int consumerID, Integer belongCompany) {
    	Integer res = MybatisManager.consumerMapper.updateBelongCompanyByID(consumerID, belongCompany);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateConsumerTypeByPhoneNum(String phoneNum, String cliType) {
    	if (phoneNum == null || phoneNum.length() != 11) {
    		return null;
    	}
    	Integer res = MybatisManager.consumerMapper.updateConsumerTypeByPhoneNum(phoneNum, cliType);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateConsumerTypeByID(int consumerID, String cliType) {
    	Integer res = MybatisManager.consumerMapper.updateConsumerTypeByID(consumerID, cliType);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateSignContractTmByID(int consumerID, Timestamp signTm) {
    	Integer res = MybatisManager.consumerMapper.updateSignContractTmByID(consumerID, signTm);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static void sendBackConsumerByID(int consumerID, int setLevel) {
    	MybatisManager.consumerMapper.sendBackConsumerByID(consumerID, setLevel);
    	MybatisManager.commit();
    }
    
    public static Integer updateConsumerFlowFollowByID(int consumerID, int setFlowFollow) {
    	Integer res = MybatisManager.consumerMapper.updateConsumerFlowFollowByID(consumerID, setFlowFollow);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static List<ConsumerInfoModel> selectConsumerByIDs(String ids) {
    	List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
    			.selectConsumerByIDs(ids);
    	for (ConsumerInfoModel consumer : consumerls) {
    		fillConsumerData(consumer);
    	}
    	
    	return consumerls;
    }
    
    public static List<ConsumerInfoModel> selectConsumerByPhoneNum(String phoneNum) {
    	List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper.selectConsumerByPhoneNum(phoneNum);
    
    	return consumerls;
    }
    
    public static List<ConsumerInfoModel> selectUserConsumerByPhoneNum(int userID, 
    		String phoneNum) {
    	List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
    			.selectUserConsumerByPhoneNum(userID,phoneNum);
        
    	return consumerls;
    }
    
    public static Integer updateSignContractTmByPhoneNum(String phoneNum, Timestamp signTm) {
    	Integer res = MybatisManager.consumerMapper.updateSignContractTmByPhoneNum(phoneNum, signTm);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static void updateConsumerUserByIDs(String ids, int toUser) {
    	MybatisManager.consumerMapper.updateConsumerUserByIDs(ids, toUser);
    	MybatisManager.commit();    	
    }
    
    public static Integer updateConsumerUserByID(int consumerID, int toUser, String setCliType) {
    	Integer res = MybatisManager.consumerMapper.updateConsumerUserByID(consumerID, toUser, setCliType);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static void updateConsumerUserLeaveOffice(int userID, int toUser) {
    	MybatisManager.consumerMapper.updateConsumerUserLeaveOffice(userID, toUser);
    	MybatisManager.commit();
    }
    
    public static List<ConsumerCarModel> selectConsumerCar(
    		int consumerID) {
    	List<ConsumerCarModel> carls = MybatisManager.consumerMapper
    			.selectConsumerCar(consumerID);
    	
    	return carls;
    }
    private static void insertConsumerCar(ConsumerCarModel carModel) {
    	MybatisManager.consumerMapper.insertConsumerCar(carModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerCar(ConsumerCarModel carModel) {
    	MybatisManager.consumerMapper.updateConsumerCar(carModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerCar(int carID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerCar(carID, consumerID);
    	MybatisManager.commit();
    }
    
    public static List<ConsumerCreditCardModel> selectConsumerCreditCard(
    		int consumerID) {
    	List<ConsumerCreditCardModel> cardls = MybatisManager.consumerMapper
    	.selectConsumerCreditCard(consumerID);
    	
    	return cardls;
    }
    private static void insertConsumerCreditCard(ConsumerCreditCardModel creditCardModel) {
    	MybatisManager.consumerMapper.insertConsumerCreditCard(creditCardModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerCreditCard(ConsumerCreditCardModel creditCardModel) {
    	MybatisManager.consumerMapper.updateConsumerCreditCard(creditCardModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerCreditCard(int creditCardID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerCreditCard(creditCardID, consumerID);
    	MybatisManager.commit();
    }
    
    public static List<ConsumerHouseModel> selectConsumerHouse(
    		int consumerID) {
    	List<ConsumerHouseModel> housels = MybatisManager.consumerMapper
    			.selectConsumerHouse(consumerID);
    	
    	return housels;
    }
    private static void insertConsumerHouse(ConsumerHouseModel houseModel) {
    	MybatisManager.consumerMapper.insertConsumerHouse(houseModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerHouse(ConsumerHouseModel houseModel) {
    	MybatisManager.consumerMapper.updateConsumerHouse(houseModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerHouse(int houseID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerHouse(houseID, consumerID);
    	MybatisManager.commit();
    }
    
    public static List<ConsumerLoanModel> selectConsumerLoan(
    		int consumerID) {
    	List<ConsumerLoanModel> loanls = MybatisManager.consumerMapper
    			.selectConsumerLoan(consumerID);
    	
    	return loanls;
    }
    private static void insertConsumerLoan(
    		ConsumerLoanModel loanModel) {
    	MybatisManager.consumerMapper.insertConsumerLoan(loanModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerLoan(ConsumerLoanModel loanModel) {
    	MybatisManager.consumerMapper.updateConsumerLoan(loanModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerLoan(int loanID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerLoan(loanID, consumerID);
    	MybatisManager.commit();
    }
    
    public static List<ConsumerPolicyModel> selectConsumerPolicy(
    		int consumerID) {
    	List<ConsumerPolicyModel> policyls = MybatisManager.consumerMapper
    			.selectConsumerPolicy(consumerID);
    	
    	return policyls;
    }
    private static void insertConsumerPolicy(
    		ConsumerPolicyModel policyModel) {
    	MybatisManager.consumerMapper.insertConsumerPolicy(policyModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerPolicy(ConsumerPolicyModel policyModel) {
    	MybatisManager.consumerMapper.updateConsumerPolicy(policyModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerPolicy(int policyID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerPolicy(policyID, consumerID);
    	MybatisManager.commit();
    }
    
    public static ConsumerSelfModel selectConsumerSelf(
    		int consumerID) {
    	ConsumerSelfModel selfCom = MybatisManager.consumerMapper
    			.selectConsumerSelf(consumerID);
    	
    	return selfCom;
    }
    private static void insertConsumerSelf(
    		ConsumerSelfModel selfModel) {
    	MybatisManager.consumerMapper.insertConsumerSelf(selfModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerSelf(ConsumerSelfModel selfModel) {
    	MybatisManager.consumerMapper.updateConsumerSelf(selfModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerSelf(int selfID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerSelf(selfID, consumerID);
    	MybatisManager.commit();
    }
    
    public static ConsumerWorkModel selectConsumerWork(
    		int consumerID) {
    	ConsumerWorkModel work = MybatisManager.consumerMapper
    			.selectConsumerWork(consumerID);
    	
    	return work;
    }
    private static void insertConsumerWork(
    		ConsumerWorkModel workModel) {
    	MybatisManager.consumerMapper.insertConsumerWork(workModel);
    	MybatisManager.commit();
    }
    private static void updateConsumerWork(ConsumerWorkModel workModel) {
    	MybatisManager.consumerMapper.updateConsumerWork(workModel);
    	MybatisManager.commit();
    }
    private static void deleteConsumerWork(int workID, int consumerID) {
    	MybatisManager.consumerMapper.deleteConsumerWork(workID, consumerID);
    	MybatisManager.commit();
    }
    
    public static List<ConsumerFollowupModel> selectConsumerFollowup(int consumerID) {
    	List<ConsumerFollowupModel> followups = MybatisManager.consumerMapper.selectConsumerFollowup(consumerID);
    	
    	return followups;
    }
    private static Integer insertConsumerFollowup(ConsumerFollowupModel followupModel) {
    	Integer res = MybatisManager.consumerMapper.insertConsumerFollowup(followupModel);
    	MybatisManager.commit();
    	
    	return res;
    }
    private static Integer insertConsumerFollowupTm(ConsumerFollowupModel followupModel) {
    	Integer res = MybatisManager.consumerMapper.insertConsumerFollowupTm(followupModel);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static List<ConsumerNextFollowupModel> selectNextFollowup(int consumerID) {
    	List<ConsumerNextFollowupModel> nextFollowupls = MybatisManager.consumerMapper.selectNextFollowup(consumerID);
    	
    	return nextFollowupls;
    }
    public static Integer insertNextFollowup(ConsumerNextFollowupModel nextFollowup) {
    	Integer res = MybatisManager.consumerMapper.insertNextFollowup(nextFollowup);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer updateNextFollowup(ConsumerNextFollowupModel nextFollowup) {
    	Integer res = MybatisManager.consumerMapper.updateNextFollowup(nextFollowup);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer deleteNextFollowup(int id) {
    	Integer res = MybatisManager.consumerMapper.deleteNextFollowup(id);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateConsumerDeleteByIDs(String strIDs) {
    	Integer res = MybatisManager.consumerMapper.updateConsumerDeleteByIDs(strIDs);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static List<ConsumerInfoModel> selectMeetTalkPurpose(String userIDs, Timestamp searchDay) {
    	List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper.selectMeetTalkPurpose(userIDs, searchDay);
    	
    	return consumerls;
    }
    
    public static List<ConsumerInfoModel> selectAllMeetTalkPurpose(Timestamp searchDay) {
    	List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper.selectAllMeetTalkPurpose(searchDay);
    	
    	return consumerls;
    }
    
    public static List<FollowupConsumerResult> selectStatisticsFollowup(String searchDay, String userIDs) {
    	List<FollowupConsumerResult> followupls = MybatisManager.consumerMapper.selectStatisticsFollowup(searchDay, userIDs);
    	
    	return followupls;
    }
    
    public static List<ConsumerInfoModel> selectAllConsumerInfoByCliType(String cliType) {
		List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
				.selectAllConsumerInfoByCliType(cliType);
				
		return consumerls;
	}
    
    
    public static List<ConsumerInfoModel>selectAllConsumerInfoNotSignedTm() {
    	List<ConsumerInfoModel> consumerls = MybatisManager.consumerMapper
    			.selectAllConsumerInfoNotSignedTm();
    	
    	return consumerls;
    }
    
    public static List<Integer> selectFollowupLTTm(Timestamp ltTm) {
    	List<Integer> resls = MybatisManager.consumerMapper.
    			selectFollowupLTTm(ltTm);
    	
    	return resls;
    }
    
    public static List<Integer> selectFollowupGTTm(Timestamp gtTm) {
    	List<Integer> resls = MybatisManager.consumerMapper.
    			selectFollowupGTTm(gtTm);
    	
    	return resls;
    }
    
    public static Integer selectMaximumConsumerID() {
    	Integer res = MybatisManager.consumerMapper.selectMaximumConsumerID();
    	
    	return res;
    }
    
    
}
